import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
from stats import Stats
from button import Button
from scoreboard import ScoreBoard
import functions as fun


def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()

    settings = Settings(screen={}, ship={}, bullet={}, alien={}, game={})
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    # 创建Play按钮
    play_button = Button(screen, "Play")

    # 创建一个用来存储游戏统计信息的实例，并创建记分牌
    stats = Stats(settings)
    score_board = ScoreBoard(settings, screen, stats)

    # 创建一艘飞船
    ship = Ship(settings, screen)

    # 创建一个子弹的编组
    bullets = Group()

    # 创建一个外星人的编组
    aliens = Group()

    # 创建外星人群
    fun.create_fleet(settings, screen, ship, aliens)

    # 开始游戏主循环
    while True:
        fun.check_events(settings, screen, stats, score_board, play_button, ship, aliens, bullets)

        if stats.game_active:
            ship.update()
            fun.update_bullets(settings, screen, stats, score_board, ship, aliens, bullets)
            fun.update_aliens(settings, screen, stats, score_board, ship, aliens, bullets)

        fun.update_screen(settings, screen, stats, score_board, ship, aliens, bullets, play_button)


run_game()
